package com.example.mq.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.mq.entity.ArticleEntity;
import com.example.mq.vo.ArticleEntityVo;
import com.example.servicebase.client.EduClient;
import com.example.servicebase.entity.EduCourse;
import com.example.mq.entity.EsCourseInfo;
import com.example.mq.service.MqService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;

@Service
public class MqServiceImpl implements MqService {
    private RestHighLevelClient client;

    @Autowired
    private EduClient eduClient;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertById(String id) {
        client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.241.128:9200")));
        //根据id从数据库中查询出数据
        EduCourse eduCourse = eduClient.getCourseById(id);
        EsCourseInfo esCourseInfo =new EsCourseInfo();
        //复制属性
        BeanUtils.copyProperties(eduCourse,esCourseInfo);
        //准备reque
        IndexRequest request =new IndexRequest("onlinelearn").id(esCourseInfo.getId());
        //准备json文档
        request.source(JSON.toJSONString(esCourseInfo), XContentType.JSON);
        try {
            client.index(request, RequestOptions.DEFAULT);
            client.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.241.128:9200")));
        DeleteRequest request =new DeleteRequest("onlinelearn",id);
        try {
            client.delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteArticleById(String id) {
        client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.241.128:9200")));
        DeleteRequest request =new DeleteRequest("article",id);
        try {
            client.delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertArticleById(String id) {
        client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.241.128:9200")));
        ArticleEntity article = mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), ArticleEntity.class, "article");
        ArticleEntityVo articleEntityVo = new ArticleEntityVo();
        BeanUtils.copyProperties(article,articleEntityVo);
        articleEntityVo.setGmtCreate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(article.getGmtCreate()));
        //准备reque
        IndexRequest request =new IndexRequest("article").id(article.getId());
        //准备json文档
        request.source(JSON.toJSONString(articleEntityVo), XContentType.JSON);
        try {
            client.index(request, RequestOptions.DEFAULT);
            client.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
